package com.bluedot.bank.action;

import java.util.Date;

import com.bluedot.bank.dao.account.Account;
import com.bluedot.bank.dao.account.AccountDao;
import com.bluedot.bank.dao.account.AccountDaoImpl;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecord;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecordDao;
import com.bluedot.bank.dao.exchangerecord.ExchangeRecordDaoImpl;
import com.opensymphony.xwork2.ActionSupport;

public class ExchangeAction extends ActionSupport {
	private ExchangeRecord recordOut=new ExchangeRecord();
	private ExchangeRecord recordIn=new ExchangeRecord();
	private long outId;
	private long inId;
	private double balance;
	public ExchangeRecord getRecordOut() {
		return recordOut;
	}
	public void setRecordOut(ExchangeRecord recordOut) {
		this.recordOut = recordOut;
	}
	public ExchangeRecord getRecordIn() {
		return recordIn;
	}
	public void setRecordIn(ExchangeRecord recordIn) {
		this.recordIn = recordIn;
	}
	public long getOutId() {
		return outId;
	}
	public void setOutId(long outId) {
		this.outId = outId;
	}
	public long getInId() {
		return inId;
	}
	public void setInId(long inId) {
		this.inId = inId;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public String execute() throws Exception {
		AccountDao accountDao=new AccountDaoImpl();
		ExchangeRecordDao recordDao=new ExchangeRecordDaoImpl();
		Account accountOut=accountDao.findById(outId);
		Account accountIn=accountDao.findById(inId);
		accountOut.setBalance(accountOut.getBalance()-this.balance);
		accountIn.setBalance(accountIn.getBalance()+this.balance);
		accountDao.update(accountOut);
		accountDao.update(accountIn);
		Date t=new Date();
		recordOut.setAccount(accountOut);
		recordOut.setAmount(balance);
		recordOut.setDescription("转出");
		recordOut.setBalance(accountOut.getBalance());
		recordOut.setExchange_time(t);
		recordDao.saveOrUpdate(recordOut);
		
		recordIn.setAccount(accountIn);
		recordIn.setAmount(balance);
		recordIn.setDescription("转入");
		recordIn.setBalance(accountIn.getBalance());
		recordIn.setExchange_time(t);
		recordDao.saveOrUpdate(recordIn);
		return SUCCESS;
	}

}
